#include <httplib.h>
#include <fstream>
#include <iostream>
#include <string>

using namespace std::placeholders;

class TEST
{
   private:
    httplib::Server *m_svr;
    void recvGetHelloHandle(const httplib::Request &req, httplib::Response &res);

   public:
    TEST();
    ~TEST()
    {
        delete m_svr;
    }

    void start(int port)
    {
        m_svr->listen("0.0.0.0", port);
    }
};

TEST::TEST()
{
    m_svr = new httplib::Server;
    httplib::Server::Handler getHello_cb = std::bind(&TEST::recvGetHelloHandle, this, _1, _2);
    m_svr->Get("/hello", getHello_cb);
}

void TEST::recvGetHelloHandle(const httplib::Request &req, httplib::Response &res)
{
    if (!req.has_header("token"))
    {
        res.set_content("fail", "text/plain");
        return;
    }

    std::string token = req.get_header_value("token");
    std::cout << "token : " << token << std::endl;
}

class TESTHTTPS
{
   private:
    httplib::SSLServer *m_svr;
    void recvGetHelloHandle(const httplib::Request &req, httplib::Response &res);

   public:
    TESTHTTPS();
    ~TESTHTTPS()
    {
        delete m_svr;
    }

    void start(int port)
    {
        m_svr->listen("localhost", port);
    }
};

TESTHTTPS::TESTHTTPS()
{
    m_svr = new httplib::SSLServer("./data/server_crt.pem", "./data/server_key.pem");

    httplib::SSLServer::Handler getHello_cb = std::bind(&TESTHTTPS::recvGetHelloHandle, this, _1, _2);
    m_svr->Get("/dd/hello", getHello_cb);
}

void TESTHTTPS::recvGetHelloHandle(const httplib::Request &req, httplib::Response &res)
{
    if (!req.has_header("token"))
    {
        res.set_content("fail", "text/plain");
        return;
    }

    std::string token = req.get_header_value("token");
    std::cout << "token : " << token << std::endl;
}

int main(void)
{
    TESTHTTPS test;
    test.start(8887);
}
